package com.tencent.qcloud.tim.uikit.modules.group.member

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.view.View.OnClickListener
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tencent.qcloud.tim.uikit.R
import com.tencent.qcloud.tim.uikit.base.ITitleBarLayout
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack
import com.tencent.qcloud.tim.uikit.component.TitleBarLayout
import com.tencent.qcloud.tim.uikit.modules.group.info.GroupInfo
import com.tencent.qcloud.tim.uikit.modules.group.info.GroupInfoProvider
import com.tencent.qcloud.tim.uikit.modules.group.interfaces.IGroupMemberLayout
import com.tencent.qcloud.tim.uikit.utils.ToastUtil

class GroupMemNewDelLayout : LinearLayout, IGroupMemberLayout {
    private var mTitleBar: TitleBarLayout? = null
    private var mMembers: RecyclerView? = null
    private var mAdapter: GroupMemberDeleteAdapter? = null
    private var mDelMembers: List<GroupMemberInfo>? = null
    private var mGroupInfo: GroupInfo? = null

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        View.inflate(context, R.layout.group_member_del_layout, this)
        mTitleBar = findViewById(R.id.group_member_title_bar)
        mTitleBar?.setTitle("移除", ITitleBarLayout.POSITION.RIGHT)
        mTitleBar?.setTitle("移除成员", ITitleBarLayout.POSITION.MIDDLE)
        mTitleBar?.getRightTitle()?.setTextColor(Color.WHITE)
        mTitleBar?.getRightIcon()?.visibility = View.GONE
        mTitleBar?.setOnRightClickListener(OnClickListener {
            val provider = GroupInfoProvider()
            provider.loadGroupInfo(mGroupInfo)
            provider.removeGroupMembers(mDelMembers, object : IUIKitCallBack {
                override fun onSuccess(data: Any) {
                    ToastUtil.toastLongMessage("删除成员成功")
                    post {
                        mTitleBar?.setTitle("移除", ITitleBarLayout.POSITION.RIGHT)
                        mAdapter!!.clear()
                        mAdapter!!.notifyDataSetChanged()
                    }
                }

                override fun onError(module: String, errCode: Int, errMsg: String) {
                    ToastUtil.toastLongMessage("删除成员失败:$errCode=$errMsg")
                }
            })
        })
        mAdapter = GroupMemberDeleteAdapter(R.layout.group_member_del_adpater, mutableListOf())
        mAdapter!!.setOnSelectChangedListener { members ->
            mDelMembers = members
            if (mDelMembers!!.size > 0) {
                mTitleBar?.setTitle("移除（" + (mDelMembers!!.size.toString() + "）"), ITitleBarLayout.POSITION.RIGHT)
            } else {
                mTitleBar?.setTitle("移除", ITitleBarLayout.POSITION.RIGHT)
            }
        }
        mMembers = findViewById(R.id.group_del_members)
        mMembers?.setLayoutManager(LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false))
        mMembers?.setAdapter(mAdapter)
    }

    override fun getTitleBar(): TitleBarLayout {
        return mTitleBar!!
    }

    override fun setParentLayout(parent: Any) {}
    override fun setDataSource(groupInfo: GroupInfo) {
        mGroupInfo = groupInfo
        mAdapter!!.setDataSource(groupInfo.memberDetails)
    }
}